#include <iostream>
#include "gameboard.h"

using namespace std;

class Best
{
    public:
    short row;
    short column;
    int score;
    bool first;
    int d;
    Best()
    {
        d=0;
        row = 0;
        column = 0;
        
        first = true;
    }
   
   
    int count_num_moves(Gameboard board);
    int chooseMove(bool black, int depth, Gameboard board, short row1, short column1,int level);
    void evaluate();
};
bool board_is_full(Gameboard board);
bool no_moves_left(Gameboard board);
int bestmove = 0;
int d;
bool first1, second;
int Best:: chooseMove(bool black, int depth, Gameboard board,short row1, short column1,int level)
{
    Gameboard full;
	int newmover=0;
	int abnew=0;
	int abbest=0;
	int score=0;
   
    if(depth == 0)
    {
		d = 1;
        for(int i=0; i<10; i++)
        {
            for(int j=0; j<10; j++)
            {
                if(board.board[i][j] == '*')
                {

                    row1 = i;
                    column1 = j;
                    newmover = chooseMove(!black,d,board,row1,column1,level);
                    if(score >= newmover || first == true)
                    {
                        first = false;
                        score = newmover;
                        row = row1;
                        column = column1;
                    }
                }
            }
        }
        return true;
    }
    if(depth < level-1)
    {
        int l = row1;
        int j = column1;
        board.move(l,j);
        board.showNextPos();
    }
    if(depth < level-1)
    {
		d++;
        for(int i=0; i<10; i++)
        {
            for(int j=0; j<10; j++)
            {
                if(board.board[i][j] == '*')
                {
                    //temporarily choose this move
                    //if()
                    row1 = i;
                    column1 = j;
					

                    newmover = chooseMove(!black,d,board,row1,column1,level);
                    if((abbest < newmover) && ((depth)%2 == 0))
                    {
                        abbest = newmover;
                    }
                    if((abbest > newmover) && ((depth)%2 == 1))
                    {
                        abbest = newmover;
                    }

                }
               
            }
        }
    }
	
	if(depth == level-1 || board_is_full(board))
    {
        abnew = count_num_moves(board);
    


		return abnew;
	}
	if(board_is_full(board))
		{
        abnew = count_num_moves(board);
    


		return 100+abnew;
	}
	return abbest;
}

int Best:: count_num_moves(Gameboard board)
{
    int p = 0;
    for(int i=1; i<10; i++)
    {
        for(int j=1; j<10; j++)
        {
            if(board.board[i][j] == '*')
            {
                p++;
               
            }
        }
    }
    return p;
}
bool board_is_full(Gameboard board)
{
    {
    int p=0;
    for(int i=0; i<10; i++)
    {
        for(int j=0; j<10; j++)
        {
            if(board.board[i][j] == '*')
            {
                p++;
            }
        }
    }


    if(p == 0 && first1 == true)
    {
        second = true;
    }
    else if(p != 0 && first1 == true)
    {
        first1 = false;
    }

    else if(p == 0)
    {
        first1 = true;
    }

    if(second == true)
    {
        return true;
    }
}
}
bool no_moves_left(Gameboard board)
{
    bool a = true;
    for(int i=1; i<=8; i++)
    {
        for(int j=1; j<=8; j++)
        {
            if(board.board[i][j] == '*')
            {
                a = false;//temporarily choose this move
                break;
            }
        }
    }
    return a;
}    